/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package plar;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.HashMap;
import plar.Actions.*;
/**
 *
 * @author spok
 */
public class Element
{
	public int id;
	public String name;
	
	public HashMap actions;
        public HashMap properties;
       
        public void newAction(String s)
        {
            actions.put(s, new Action(this,s));
        }
        
        public void newAction(String s,Action a)
        {
            actions.put(s,  a);
        }
                
        public Action setActiveAction(String s)
        {
            Action a = getAction(s);
            HMActiveAction().put(s, a);
            return a;
            
        }
        public boolean doAction(String a,Event e)
        {
            return setActiveAction(a).doAction(e);
        }
        public Action unsetActiveAction(String s)
        {
            Action a = getAction(s);
            HMActiveAction().remove(s);
            return a;
        }

        public Action getAction(String s)
        {
            return (Action)actions.get(s);
        }
        
        public Sprite getCurrentSprite()
        {
          HashMap <String,Action> activeActions = 
                  (HashMap <String,Action>)properties.get("activeActions");
          Sprite currentSprite = new Sprite("");
          for(String key:activeActions.keySet())
          {
            Action a = getAction(key);
            Sprite s;
            if((s = a.getCurrentSprite())!=null && !s.filename.isEmpty())
                currentSprite = s;
                
          }
          
          return (currentSprite.filename.equals(""))
                    ?null
                    :currentSprite;
          
          
        }
        
        public void clock()
        {
            HashMap <String,Action> activeActions = HMActiveAction();
            for(String key : activeActions.keySet())
                    
            {
                Action a = getAction(key);
                a.clock();
            }
        }
          
        public HashMap <String,Action> HMActiveAction()
        {
            return (HashMap <String,Action>)properties.get("activeActions");
        }
        
   	public Element(int i)
	{

		actions =  new HashMap();
                newAction("show",new ElementShow());
                properties =  new HashMap();
                id = i;
                properties.put("position",new Rectangle());
                properties.put("speed",new Point(0,0));
                properties.put("activeActions", new HashMap <String,Action>());
                setActiveAction("show");
                properties.put("type",ElementType.Unknown);
               
                
	}
	
	public Element(int i,Rectangle pos)
	{
		this(i);
		properties.put("position",pos);
		
	}

        
        public Element()
        {
            this(-1);
        }
/*

	boolean Inside(Element e,boolean b)
	{
		return (RightOf(e,b) && LeftOf(e,b) && TopOf(e,b) && BottomOf(e,b));
	}

	boolean Join(Element e,boolean b)
	{
		return (XInside(e,b) && YInside(e,b));
	}

	boolean RightOf(Element e,boolean b)
		if(b) 
			return (this.Pointz.x
	}

	
*/
         public Rectangle getPosition()
         {
          return (Rectangle)properties.get("position");
         }
         
         public void setPosition(Point p)
         {
             ((Rectangle)properties.get("position")).x=p.x;
             ((Rectangle)properties.get("position")).y=p.y;
         }
         public void setSize(Point p)
         {
             ((Rectangle)properties.get("position")).width=p.x;
             ((Rectangle)properties.get("position")).height=p.y;
         }
            public void setSize(int x,int y)
         {
             ((Rectangle)properties.get("position")).width=x;
             ((Rectangle)properties.get("position")).height=y;
         }     
           public boolean contains(Point point)
           {
               Rectangle pos = getPosition();
            int leftSide = pos.x;
	    int rightSide = pos.x+pos.width;
	    int top = pos.y;
	    int bottom = pos.y+pos.height;
	    
            
            if(point.x <= rightSide)
	        if(point.x >= leftSide)
	        	if(point.y <= bottom)
	        		if(point.y >= top)
	        			return true;
            return false;
           }

	public boolean isInside(Element e,boolean b)
	{
		return true;
	}
        
        public boolean isOutside(Element e,boolean b)
	{
		return !isInside(e,b);
	}
	public boolean join(Element e,boolean b)
	{
		return  true;
	}
        
        public String toString()
        {
            return "[Element [id="+(id)+"] [pos="+((Point)properties.get("position"))+"]]";
        }
}

